package com.example.m_kartupay;
import java.util.Calendar;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.m_kartupay.library.DatabaseHandler;
import com.example.m_kartupay.library.InquiryDatabaseHandler;
import com.example.m_kartupay.library.StrictWrapper;
import com.example.m_kartupay.library.UserFunctions;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
import android.widget.TextView;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MyAccountActivity extends Activity implements
OnClickListener {
	UserFunctions userFunctions;
	HashMap<String, String> account;
	TextView account_number,balance,point;
	// var content form
	EditText etfullname, etemail, etnocard,etaddress,etcity, ethandphone,etplaceofbirth;
	
	private int mYear, mMonth, mDay;
	EditText etDisplayExpiredDate,etDisplayBirthDayDate;
	
	TextView tvDisplayTime;
	ImageButton imgbCollap1,imgbCollap2;	
	LinearLayout  lyCollap1,lyCollap2;	
	Button dtexpired,dtbirthday;
	
	static final int TIME_DIALOG_ID = 999;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {       
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_account);	
		StrictWrapper.init();
		final int DATE_DIALOG_ID = 0;
		account_number = (TextView)findViewById(R.id.tvAccountNumber);
		balance = (TextView)findViewById(R.id.tvBalance);
		point = (TextView)findViewById(R.id.tvPoint);
		
		etfullname = (EditText)findViewById(R.id.etFullName);
		etemail = (EditText)findViewById(R.id.etEmail);
		etnocard = (EditText)findViewById(R.id.etNoKtp);
		etaddress = (EditText)findViewById(R.id.etAddress);
		etcity = (EditText)findViewById(R.id.etCity);
		ethandphone = (EditText)findViewById(R.id.etHandphone);
		etplaceofbirth =(EditText)findViewById(R.id.etPlaceOfBirth);
		
		
		Intent inMainMenu = getIntent();
		Bundle b = inMainMenu.getExtras();
		String acc = (String)b.get("account_number");
		String balc = (String)b.get("balance");
		String poi = (String)b.get("points");
		account_number.setText(acc);
		balance.setText(balc);
		point.setText(poi);
		
		etDisplayExpiredDate = (EditText)findViewById(R.id.etExpiredDate);
		etDisplayBirthDayDate = (EditText)findViewById(R.id.etDateOfBirth);
		dtexpired = (Button)findViewById(R.id.btnExpiredDate);
		dtbirthday = (Button)findViewById(R.id.btnPlaceOfBirth);  
		
		imgbCollap1 = (ImageButton)findViewById(R.id.imgbCollap1);
		imgbCollap2 = (ImageButton)findViewById(R.id.imgbCollap2);
		lyCollap1 = (LinearLayout )findViewById(R.id.lycollap1);
		lyCollap2 = (LinearLayout )findViewById(R.id.lycollap2);
		//lyCollap1.setVisibility(View.GONE);;
	    final Spinner spCard = (Spinner) findViewById(R.id.spIdCard);
		final Spinner spGender = (Spinner) findViewById(R.id.spGender);
		
		ArrayAdapter<String> adapterCard = new ArrayAdapter<String>(this,
			        android.R.layout.simple_spinner_item, arrcard);
		adapterCard.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spCard.setAdapter(adapterCard);
		
		ArrayAdapter<String> adapterGender = new ArrayAdapter<String>(this,
		        android.R.layout.simple_spinner_item, arrgender);
		adapterGender.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spGender.setAdapter(adapterGender);
	
		
		dtexpired.setOnClickListener(this);
	    dtbirthday.setOnClickListener(this);
	    // Koneksi json get detail data user
	     userFunctions = new UserFunctions();
		   
	     DatabaseHandler db = new DatabaseHandler(getApplicationContext());
	 	 account = db.getUserDetails();
	     String channel_id = "123456789";
	 	 String email = (String)account.get("email");
	 	 String user_id = (String)account.get("user_id");
		
		 JSONObject jsonUserDetail = userFunctions.userdetail(channel_id, user_id, email);
		 Log.d("JSON UserDetail",jsonUserDetail.toString());
		
		  String res;
			try {
				res = jsonUserDetail.getString("responseCode");
				if(Integer.parseInt(res) ==0)
				  {
					 JSONObject object = jsonUserDetail.getJSONObject("data");
					 etfullname.setText(object.getString("name"));
					 etemail.setText(object.getString("email"));
					 etnocard.setText(object.getString("id_number"));
					 etaddress.setText(object.getString("address"));
					 etcity.setText(object.getString("city"));
					 ethandphone.setText(object.getString("phone"));
					 etplaceofbirth.setText(object.getString("place_of_birth"));
					 etDisplayExpiredDate.setText(object.getString("id_expired_date"));
					 etDisplayBirthDayDate.setText(object.getString("date_of_birth"));
					
				  }
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		  
		imgbCollap1.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	lyCollap1.setVisibility( lyCollap1.isShown()
                        ? View.GONE
                        : View.VISIBLE );
	        	
	        }
	    });	
		imgbCollap2.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	lyCollap2.setVisibility( lyCollap2.isShown()
                        ? View.GONE
                        : View.VISIBLE );
	        	
	        }
	    });		
		
		
	}
	 static final String[] arrcard = new String[] { "KTP", "SIM",
     "PASSPORT" };
	 static final String[] arrgender = new String[] { "MALE", "FEMALE"};
    @Override
	 public void onClick(View v) {

			if (v == dtexpired) {

				// Process to get Current Date
				final Calendar c = Calendar.getInstance();
				mYear = c.get(Calendar.YEAR);
				mMonth = c.get(Calendar.MONTH);
				mDay = c.get(Calendar.DAY_OF_MONTH);

				// Launch Date Picker Dialog
				DatePickerDialog dpd = new DatePickerDialog(this,
						new DatePickerDialog.OnDateSetListener() {

							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								// Display Selected date in textbox
								etDisplayExpiredDate.setText(dayOfMonth + "-"
										+ (monthOfYear + 1) + "-" + year);

							}
						}, mYear, mMonth, mDay);
				dpd.show();
			}
			if (v == dtbirthday) {

				// Process to get Current Date
				final Calendar c = Calendar.getInstance();
				mYear = c.get(Calendar.YEAR);
				mMonth = c.get(Calendar.MONTH);
				mDay = c.get(Calendar.DAY_OF_MONTH);

				// Launch Date Picker Dialog
				DatePickerDialog dpd = new DatePickerDialog(this,
						new DatePickerDialog.OnDateSetListener() {

							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								// Display Selected date in textbox
								etDisplayBirthDayDate.setText(dayOfMonth + "-"
										+ (monthOfYear + 1) + "-" + year);

							}
						}, mYear, mMonth, mDay);
				dpd.show();
			}
			
	 }
	

}
